﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using MyVoteMVC.Web.Models;
using MyVoteMVC.Service;
using MyVoteMVC.Common;

namespace MyVoteMVC.Web.Controllers
{
    public class AccountController : VoteBaseController
    {

        public IFormsAuthenticationService FormsService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }

            base.Initialize(requestContext);
        }

        // **************************************
        // URL: /Account/LogOn
        // **************************************

        public ActionResult LogOn()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(LogOnModel userModel, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                using (IUserService userService = Factory.GetInstance<IUserService>())
                {
                    var pUser = userService.UserLogin(userModel.UserName, userModel.Password);

                    if (pUser != null)
                    {
                        FormsService.SignIn(pUser.User_Name, pUser.User_ID);

                        if (!String.IsNullOrEmpty(returnUrl))
                        {
                            return Redirect(returnUrl);
                        }
                        else
                        {
                            return RedirectToAction("Index", "Home");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "用户名或NT账号错误");
                    }
                }

            }
            return View(userModel);
        }

        // **************************************
        // URL: /Account/LogOff
        // **************************************

        public ActionResult LogOff()
        {
            FormsService.SignOut();

            return RedirectToAction("Index", "Home");
        }

        // **************************************
        // URL: /Account/Register
        // **************************************

        public ActionResult Register()
        {            
            return View();
        }

        [HttpPost]
        public ActionResult Register(RegisterModel userModel)
        {
            if (ModelState.IsValid)
            {
                using (IUserService userService = Factory.GetInstance<IUserService>())
                {
                    MyVoteMVC.Domain.VT_Users pUser;
                    var pResult = userService.Register(userModel.UserName, userModel.UserName, userModel.UserName, out pUser);

                    if (pResult == 1)
                    {
                        //FormsService.SignIn(pUser.User_Name, pUser.User_ID);
                        return RedirectToAction("RegisterSuccess", "Account");
                    }
                    else if (pResult == -1)
                    {
                        ModelState.AddModelError("", "邮件发送失败");
                    }
                    else if (pResult == 2)
                    {
                        ModelState.AddModelError("", "已存在同名账号");
                    }
                    else
                    {
                        ModelState.AddModelError("", "新增用户失败");
                    }
                }                
            }
            return View(userModel);
        }

        public ActionResult RegisterSuccess()
        {
            return View();
        }

        // **************************************
        // URL: /Account/ChangePassword
        // **************************************

        [Authorize]
        public ActionResult ChangePassword()
        {           
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult ChangePassword(ChangePasswordModel userModel)
        {
            if (ModelState.IsValid)
            {
                using (IUserService userService = Factory.GetInstance<IUserService>())
                {                    
                    if (userService.ChangePassword(User.Identity.Name, userModel.OldPassword, userModel.NewPassword))
                        return RedirectToAction("ChangePasswordSuccess");
                    else
                        ModelState.AddModelError("", "修改密码失败");
                }
            }
            return View(userModel);
        }

        // **************************************
        // URL: /Account/ChangePasswordSuccess
        // **************************************

        public ActionResult ChangePasswordSuccess()
        {
            return View();
        }

        public ActionResult ForgetPassword()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ForgetPassword(RegisterModel userModel)
        {
            if (ModelState.IsValid)
            {
                using (IUserService userService = Factory.GetInstance<IUserService>())
                {
                    if (userService.ForgotPassword(userModel.UserName))
                        return RedirectToAction("ForgetPasswordSuccess");
                    else
                        ModelState.AddModelError("", "修改密码失败");
                }
            }
            return View(userModel);
        }

        public ActionResult ForgetPasswordSuccess()
        {
            return View();
        }

    }
}
